package com.emergency.util;

import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.sms.SmsClient;
import com.baidubce.services.sms.SmsClientConfiguration;
import com.baidubce.services.sms.model.SendMessageV3Request;
import com.baidubce.services.sms.model.SendMessageV3Response;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Component
public class SMSUtil {

    private static SMSUtil smsUtil;

    @PostConstruct
    public void init() {
        smsUtil = this;
        smsUtil.accessKeyId = this.accessKeyId;
        smsUtil.secretAccessKey = this.secretAccessKey;
        smsUtil.endpoint = this.endpoint;
        smsUtil.startTemplate = this.startTemplate;
        smsUtil.dispatchTemplate = this.dispatchTemplate;
    }

    @Value("${baiduSMS.ACCESS_KEY_ID}")
    private String accessKeyId;

    @Value("${baiduSMS.SECRET_ACCESS_KEY}")
    private String secretAccessKey;

    @Value("${baiduSMS.ENDPOINT}")
    private String endpoint;

    @Value("${baiduSMS.INVOKEID}")
    private String signatureId;

    @Value("${baiduSMS.START_TEMPLATE}")
    private String startTemplate;

    @Value("${baiduSMS.DISPATCH_TEMPLATE}")
    private String dispatchTemplate;

    public static boolean sendExpertSMS(String phone, String affectedPointName, String linkAddress) {
        Map<String, String> contentVar = new HashMap<>();
        SendMessageV3Request request = new SendMessageV3Request();
        request.setTemplate(smsUtil.dispatchTemplate);
        contentVar.put("sn_1", affectedPointName);
        contentVar.put("sn_2",linkAddress);
        return send(phone, contentVar, request);
    }

    public static boolean sendStartResponseSMS(String phone, Integer level, String linkAddress) {
        Map<String, String> contentVar = new HashMap<>();
        switch (level) {
            case 1:
                contentVar.put("sn_1", "一级响应");
                break;
            case 2:
                contentVar.put("sn_1", "二级响应");
                break;
            case 3:
                contentVar.put("sn_1", "三级响应");
                break;
            case 4:
                contentVar.put("sn_1", "四级响应");
                break;
        }
        contentVar.put("sn_2",linkAddress);
        SendMessageV3Request request = new SendMessageV3Request();
        request.setTemplate(smsUtil.startTemplate);
        return send(phone, contentVar, request);
    }

    private static boolean send(String phone, Map<String, String> contentVar, SendMessageV3Request request) {
        SmsClientConfiguration config;
        config = new SmsClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(smsUtil.accessKeyId, smsUtil.secretAccessKey));
        config.setEndpoint(smsUtil.endpoint);
        SmsClient client = new SmsClient(config);
        request.setMobile(phone);
        request.setSignatureId(smsUtil.signatureId);
        request.setContentVar(contentVar);
        SendMessageV3Response response = client.sendMessage(request);
        // 解析请求响应 response.isSuccess()为true 表示成功
        return response != null && response.isSuccess();
    }
}
